package com.yllt4cloud.common.ddbase.util;

import org.apache.commons.lang3.StringUtils;

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;


public class StringUtil {

    /**
     * 首字母转大写
     * @param s
     * @return
     */
    public static String firstToUpper( String s )
    {
        if ( s == null || s.length() == 0 )
        {
            return s;
        }
        if ( Character.isUpperCase( s.charAt( 0 ) ) )
            return s;
        else
            return ( new StringBuilder() ).append( Character.toUpperCase( s.charAt( 0 ) ) ).append( s.substring( 1 ) ).toString();
    }

    /**
     * 字符串数组转int数组
     * @param strs
     * @return
     * @return int[]
     */
    public static int[] convertStrsToInts( String[] strs )
    {
        int s3[] = new int[strs.length];

        for ( int i = 0; i < strs.length; i++ )
        {
            s3[i] = Integer.parseInt( strs[i] );// 将字符串数组转换成整型,结果出现异常。后面事件处理时，调用sort,出现错误。*/
        }

        return s3;
    }
    /**
     * 
     * <p><b>作用：</b></p>
     * <p><b>日期：</b>2018-7-25 上午11:15:11</p>
     * <p><b>作者：</b>池建松</p>
     * @param str
     * @return
     * @exception
     */
    public static int[] StringtoInt(String str) {  
    	  
        int ret[] = new int[str.length()];   
      
        StringTokenizer toKenizer = new StringTokenizer(str, ",");   
      
        int i = 0;  
      
        while (toKenizer.hasMoreElements()) {   
      
          ret[i++] = Integer.valueOf(toKenizer.nextToken());  
      
        }   
      
       return ret;  
      
     }  
    public static List<Integer> StringtoListInteger(String str) {  
		List<Integer> intTerms = new ArrayList<>();
    	if(str!=null && !"".equals(str)){
			List<String> terms = Arrays.asList(StringUtils.split(str, ","));
			for (String term : terms) {
			    intTerms.add(Integer.parseInt(term));
			}
			return intTerms;
		}
    	return intTerms;
     }  
	/*
	*
	* 数字不足位数左补0
	*
	* @param str
	* @param strLength
	*/
	public static String addZeroForNum(String str, int strLength) {
		int strLen = str.length();
		if (strLen < strLength) {
		while (strLen < strLength) {
		StringBuffer sb = new StringBuffer();
		sb.append("0").append(str);//左补0
		// sb.append(str).append("0");//右补0
		str = sb.toString();
		strLen = str.length();
		}
		}
		return str;
	}
	
	
	/**
	 * 字符串替换成特殊字符
	 */
	public static String replaceStr(String Str ,String toStr ,int began ,int end){
		String beganStr = Str.substring(0, began);
		String endStr = Str.substring(end,Str.length());
		String toStrs = "";
		for(int i = 0 ; i < (end - began); i++){
			toStrs += toStr;
		}
		return beganStr+toStrs+endStr;
	}
	
	
	public static String miniStr(String str,int num){
		if(str == null){
			return "";
		}
		if(str.length() < num){
			return str;
		}else{
			return str.substring(0, num)+"...";
		}
	}

    /**
     * 是否为空（"     "不为空）
     * @param
     * @return
     */
    public static boolean isEmpty( String str )
    {
        return ( ( str == null ) || ( str.length() == 0 ) );
    }

    /**
     * 是否空白（"     "也为空）
     * @param
     * @return
     */
    public static boolean isBlank( String str )
    {
        int length;

        if ( ( str == null ) || ( ( length = str.length() ) == 0 ) )
        {
            return true;
        }

        for ( int i = 0; i < length; i++ )
        {
            if ( !Character.isWhitespace( str.charAt( i ) ) )
            {
                return false;
            }
        }

        return true;
    }
    
    
    public static String formatDouble(double d) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        // 保留两位小数
        nf.setMaximumFractionDigits(2); 

        // 如果不需要四舍五入，可以使用RoundingMode.DOWN
        nf.setRoundingMode(RoundingMode.UP);

        return nf.format(d);
    }
}
